package Prototypes;

import javax.swing.*;
import java.awt.*;

/**
 * Component to be used as tabComponent;
 * contains two JLabels to show the title and the icon
 * for each tab, takes title and icon from the JTabbedPane
 * so always shows up-to-date information
 *
 * @author Alexander Potochkin
 */
public class LabelTabComponent extends JPanel {
    private final JTabbedPane pane;
    private final JLabel textLabel = new TabLabel(true);
    private final JLabel secondIconLabel = new TabLabel(false);
    private Font font = new Font(Font.DIALOG, Font.PLAIN, 15);

    public LabelTabComponent(JTabbedPane pane) {
        super(new FlowLayout(FlowLayout.LEFT, 5, 0));
        if (pane == null) {
            throw new NullPointerException("JTabbedPane is null");
        }
        this.pane = pane;
        setOpaque(false);
        textLabel.setFont(font);
        add(textLabel);
        add(secondIconLabel);
    }

    class TabLabel extends JLabel {
        private boolean showText;

        public TabLabel(boolean showText) {
            this.showText = showText;
        }

        public String getText() {
            if (showText) {
                int i = pane.indexOfTabComponent(LabelTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                }
            }
            return null;
        }

        public Icon getIcon() {
            int i = pane.indexOfTabComponent(LabelTabComponent.this);
            if (i != -1) {
                return pane.getIconAt(i);
            }
            return null;
        }
    }
}